﻿<%@ WebService Language="C#" Class="VantageWS" %>

using System;
using System.Data.Linq;
using N = System.Net;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using V = Vantage.WS.Client;
using VWS = Vantage.WS;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class VantageWS : System.Web.Services.WebService {

	[WebMethod]
	public string NewApp(int LoanID) {
		var ws = new com.vantageilm.services.Origination();
		System.Net.ServicePointManager.CheckCertificateRevocationList = false;
		Data.LINQ.MotoBank db = new Data.LINQ.MotoBank();
		var til = db.v_TruthInLendings.Single(s => s.LoanID == LoanID);
		var ci = db.v_CarInfos.Single(s => s.LoanID == LoanID);
		var b0 = db.Borrower(LoanID,0).Single();
		var A00 = db.v_Addresses.Single(s => s.BorrowerID == b0.ID && s.AddressPos == 0 && s.LoanID==LoanID);
		var E00 = db.t_Employments.Single(s=>s.BorrowerID==b0.ID && s.Pos==0 && s.JobNumber==0);
		V.DSs.NewAppRequest dsNA = new Vantage.WS.Client.DSs.NewAppRequest();
		var rowLoan = dsNA.Loan.AddLoanRow();
		var rowCreditApp = dsNA.CreditApplication.AddCreditApplicationRow(rowLoan);
		dsNA.Collateral.AddCollateralRow(rowCreditApp,"", "", til.VIN+"", til.BookValue + "", "BlackBook", ci.Make+"", ci.Model+"", ci.Year + "",
			"", "", "", ci.Mileage + "", "","", "", "", "", "", "", "", "", "", "", "",
			til.ContractDate_F, til.SalesPrice + "", "", til.ContractDate_F, "", "");
		var rowApplicant = dsNA.Applicant.AddApplicantRow(rowCreditApp, "", "", b0.FirstName + "", "", "", b0.LastName + "", "", "", "", b0.SSN + "", b0.DOB + "",
			"", "", "", "", b0.GrossIncome + "", "", b0.NonEmploymentIncome + "", "", b0.NonEmploymentIncomeDesc + "", "", "", A00.LandLord + "",
			"", b0.DriverLicense + "", "", b0.Language + "", "", "", "");
		dsNA.CurrentAddress.AddCurrentAddressRow(rowApplicant, "", A00.Street + "", "", "", "", "", "", "", A00.CityName + "", A00.StateCode + "",
			(A00.Zip + "").Substring(0, 5), "", A00.Years + "", A00.Months + "", "", "", "", "", "", "", "");
		dsNA.CurrentEmployment.AddCurrentEmploymentRow(rowApplicant, "", E00.Position + "", "", "", E00.Years + "", E00.Months + "", "",
			"", "", "", "");
		dsNA.HomePhone.AddHomePhoneRow(rowApplicant, b0.PhoneHome+"", "", "", "", "", "");
		dsNA.KeyData.AddKeyDataRow(rowLoan,"", "", "XXX", "Dimok", "", "", "", "", "", "");
		string req = dsNA.GetXml();
		N.WebClient wc = new System.Net.WebClient();
		wc.BaseAddress = "https://services.vantageilm.com/Origination.asmx?op=VantageILMNewApp";
		var qs = new System.Collections.Specialized.NameValueCollection();
		qs.Add("auth", "9D9800F21E3E76D0B7065BCB8CD2FC57");
		System.Net.ServicePointManager.ServerCertificateValidationCallback =
			new System.Net.Security.RemoteCertificateValidationCallback( ValidateServerCertificate);
		//string resp = System.Text.Encoding.ASCII.GetString(wc.UploadValues("","POST",qs));
		ws.VantageILMNewApp("9D9800F21E3E76D0B7065BCB8CD2FC57", req);
		return req;
	}
	public static bool ValidateServerCertificate(
			object sender,
			System.Security.Cryptography.X509Certificates.X509Certificate certificate,
			System.Security.Cryptography.X509Certificates.X509Chain chain,
			System.Net.Security.SslPolicyErrors sslPolicyErrors) {
			return true;
	}



	class CertificatePolicy : N.ICertificatePolicy {
		#region ICertificatePolicy Members

		public bool CheckValidationResult(System.Net.ServicePoint srvPoint, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Net.WebRequest request, int certificateProblem) {
			return true;
		}

		#endregion
	}
}

